/*
 * @Author       : 魏威 <1209562577@qq.com>
 * @Date         : 2022-02-22 17:13 周2
 * @Description  : 未授权组件
 */

import { Button, Card, Divider, Typography } from "antd";
import React, { useState } from "react";
import LoginScreen from "./login";
import RegisterScreen from "./register";
import styled from "@emotion/styled";

import logo from "../assets/logo.svg";
import left from "../assets/left.svg";
import right from "../assets/right.svg";
import { useDocmentTitle } from "hooks";

export default function UnauthenticatedApp() {
  const [isRegister, setIsRegister] = useState(false);
  const [error, setError] = useState<Error | null>(null);

  // useDocmentTitle(isRegister ? "注册" : "登录");

  return (
    <Container>
      <Background />
      <Header />
      <ShadowCard style={{ width: 400 }}>
        <Title>{isRegister ? "请注册" : "请登录"}</Title>
        {isRegister ? (
          <RegisterScreen onError={setError} />
        ) : (
          <LoginScreen onError={setError} />
        )}
        {error && (
          <Typography.Text type={"danger"}>{error.message}</Typography.Text>
        )}
        <Divider />
        <Button
          type={"link"}
          onClick={() => {
            setIsRegister(!isRegister);
            setError(null);
          }}
        >
          切换到{isRegister ? "登录" : "注册"}
        </Button>
      </ShadowCard>
    </Container>
  );
}

const Container = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
`;

const Background = styled.div`
  position: absolute;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: left bottom, right bottom;
  background-size: calc(((100vw - 40rem) / 2) - 3.2rem),
    calc(((100vw - 40rem) / 2) - 3.2rem);
  background-image: url(${left}), url(${right});
`;

const Header = styled.header`
  background: url(${logo}) no-repeat center;
  padding: 5rem 0;
  background-size: 8rem;
  width: 100%;
`;

const ShadowCard = styled(Card)`
  width: 40rem;
  min-height: 40rem;
  padding: 3.2rem;
  box-sizing: border-box;
  box-shadow: rgba(0, 0, 0, 0.1) 0 0 10px;
`;

const Title = styled.h2`
  text-align: center;
  font-weight: bold;
  margin-bottom: 2.4rem;
  color: rgb(113, 153, 109);
`;
